package id.ac.gunadarma.paracorp.repository;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import id.ac.gunadarma.paracorp.bean.CollectionNameBean;
import id.ac.gunadarma.paracorp.model.TranslatedSentence;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

@Repository
public class TranslatedSentenceRepository {
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Autowired
	private CollectionNameBean collectionNameBean;
	
	public TranslatedSentence findById(String id, String language) {
		TranslatedSentence result = null;
		
		Query query = new Query(where("id").is(id));
		result = mongoTemplate.findOne(query, TranslatedSentence.class, getCollectionName(language));
	
		return result;
	}
	
	public List<TranslatedSentence> findAll(String language, Pageable pageable) {
		List<TranslatedSentence> resultList = null;
		
		Query query = new Query(where("id").exists(true)).with(pageable);
		resultList = (List<TranslatedSentence>) 
				mongoTemplate.find(query, TranslatedSentence.class, getCollectionName(language));
		
		return resultList;
	}
	
	public TranslatedSentence save(TranslatedSentence objectToSave) {
		mongoTemplate.save(objectToSave, this.getCollectionName(objectToSave.getLanguage()));
		
		return objectToSave;
	}
	
	public long getCount(String language) {
		long count = 0;
		
		count = mongoTemplate.getCollection(this.getCollectionName(language)).count();
		
		return count;
	}
	
	public String getCollectionName(String language) {
		return collectionNameBean.getCollectionName(language);
	}

}
